<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人中心</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- Card区域 -->
    <el-card class="box-card">
      <h3>{{addUserForm.name}}</h3>
      <el-divider></el-divider>
      <span>用户类型:{{addUserForm.type}}</span>
      <el-divider></el-divider>
      <span>创建于:{{formatTime}}</span>
      <el-divider></el-divider>
      <el-form
        :model="addUserForm"
        :rules="rules"
        ref="addUserFormRef"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <el-form-item label="用户名" prop="username">
                <el-input v-model="addUserForm.username" disabled></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <el-form-item label="电话" prop="mobile">
                <el-input v-model="addUserForm.mobile"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="addUserForm.name"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button type="success" @click="submitForm('ruleForm')"
            >保存</el-button
          >
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { userSelf } from 'api/profile'

export default {
  name: 'Profile',
  data () {
    return {
      dialogVisible: false,
      addUserForm: {
        username: '',
        password: '',
        created: '',
        name: '',
        mobile: '',
        type: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'change' },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'change'
          }
        ],
        phone: [{ required: true, message: '手机不能为空', trigger: 'change' }]
      }
    }
  },
  created () {
    this.getUserSelf()
  },
  computed: {
    formatTime: function () {
      const time = this.addUserForm.created
      return time.replace('T', ' ').slice(0, 19)
    }
  },
  methods: {
    getUserSelf () {
      userSelf()
        .then(res => {
          if (res.data.code !== 0) {
            return this.$message.error('获取信息失败！')
          }
          console.log(res)
          this.addUserForm = res.data.data
          this.$message.success('信息获取成功！')
        })
        .catch(err => {
          console.log(err)
          return this.$message.error('获取信息失败！')
        })
    },
    resetForm () {
      this.$refs.addUserFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.createdTime{
  font-size: 14px;
}
</style>
